/** 圆桌骑士,为了保证出招效果,必须记录按键1的按下情况

  By setycyas @2025-07-15
*/

/* 全局设定
*/
#Warn
#MaxThreadsPerHotkey 1
SetWorkingDir(A_ScriptDir)
;导入自定义函数库
#Include "..\Class\OrderMap.2.ahk"
#Include "..\Class\MyFuns.2.ahk"
;导入宏相关函数和UI
#Include "..\Class\FaceDetector.2.ahk"
#Include "..\Class\CommonMacro.2.ahk"
#Include "..\Class\CommonUI.2.ahk"
#Include "..\Class\MyEmu.2.ahk"

SendMode("Input")

/* 制作圆桌骑士的宏类
*/
class KnightsMacro extends CommonMacro {

  pressDuration := 30
  aFlag := False ;a键按下与否的标记
  
  /* 新增函数 
  */
  ;按键1的状态需要记录,以防大斩防御等操作出现问题,所以需要相关函数
  ;按下按键1并记录状态
  _key1Down(){
    if (!this.aFlag) {
      this._down(1)
      this.aFlag := True
    }
  }
  _key1Up(){
    if (this.aFlag) {
      this._up(1)
      this.aFlag := False
    }
  }
  ;清除按键1的按下状态,如果需要清除,等待一下保证清除效果
  _clearKey1(){
    if this.aFlag {
      this._key1Up()
      sleep(20)
    }
  }
  
  /* 宏函数
  */
  ;改装后的连发,要求第一击延时较长保证立即接其他招
  _连发A(key){
    local vk := GetKeyVK(MyFuns.getHotkey(key))
    local delay1 := this.testParams[1]
    this._key1Down()
    sleep(30)
    this._key1Up()
    if (!MyFuns.getVkState(vk)) {
      return
    } else {
      sleep(delay1)
    }
    while (MyFuns.getVkState(vk)) {
      this._key1Down()
      sleep(30)
      this._key1Up()
      sleep(30)
    }
  }
  _大斩(key){
    local face := this._face.face()
    this._clearKey1()
    this._key1Down()
    sleep(50)
    MyFuns.keyDown(face)
    sleep(100)
    this._key1Up()
    MyFuns.keyUp(face)
  }
  _防御(key){
    ;local maxDefTime := this.testParams[1] ;850
    local maxDefTime := 850
    local back := this._face.back()
    local vk := GetKeyVK(MyFuns.getHotkey(key))
    this._clearKey1()
    this._key1Down()
    sleep(50)
    MyFuns.keyDown(back)
    MyFuns.waitVkUp(vk, maxDefTime, 30)
    this._key1Up()
    MyFuns.keyUp(back)
    while (MyFuns.getVkState(vk)) {
      sleep(30)
    }
  }
  _A上(key){
    this._clearKey1()
    this._key1Down()
    sleep(50)
    MyFuns.keyDown("up")
    sleep(100)
    this._key1Up()
    MyFuns.keyUp("up")
  }
  _前前A(key){
    local face := this._face.face()
    MyFuns.press(face, this.testParams[1]) ;30
    sleep(this.testParams[2]) ;60
    ;MyFuns.press(face, this.testParams[3])
    MyFuns.keyDown(face)
    sleep(this.testParams[3]) ;400
    this._key1Down()
    sleep(this.pressDuration)
    this._key1Up()
    sleep(100)
    MyFuns.keyUp(face)
  }
  _下马(key){
    ;local delay1 := this.testParams[2] ;12帧,180
    local delay1 := 180
    this._down(2)
    sleep(30)
    this._up(2)
    sleep(delay1)
    this._press(3)
  }
  /*
  _转身斩(key){
    local back := this._face.back()
    MyFuns.press(back, 50)
    this._press(1)
    this._face.setFace(back)
  }
  */
  _转身斩(key){
    local face := this._face.face()
    local back := this._face.back()
    MyFuns.press(back, 30)
    sleep(30)
    MyFuns.keyDown(face)
    sleep(30)
    this._key1Down()
    sleep(this.pressDuration)
    this._key1Up()
    MyFuns.keyUp(face)
  }
  

  /* 重载宏接口
  */
  _setMacro(){
    static obm := ObjBindMethod 
    this.macroMap := OrderMap()
    this.macroMap["连发A"] := obm(this, "_连发A")
    this.macroMap["大斩"] := obm(this, "_大斩")
    this.macroMap["防御"] := obm(this, "_防御")
    this.macroMap["下马"] := obm(this, "_下马")
    this.macroMap["A上"] := obm(this, "_A上")
    this.macroMap["前前A"] := obm(this, "_前前A")
    this.macroMap["转身斩"] := obm(this, "_转身斩")
    this.macroArr := this.macroMap.getKeys()
  }

}

/* 制作圆桌骑士专用UI
*/
class KnightsUI extends CommonUI {

  EXE_ARRAY := [MyEmu.MAME, MyEmu.FBAS, MyEmu.XZONE]

  /* 构造函数
  */
  __New(macro){
    super.__New("圆桌骑士", macro)
    this._preset := OrderMap()
    ;this._preset["默认"] := ["大斩", "A上", "防御", "下马"]
    this._preset["兰斯洛特"] := ["连发A", "大斩", "防御", "转身斩"]
    this._preset["斧头男"] := ["连发A", "大斩", "防御", "前前A"]
  }

  /* 重写接口
  */

  /* 重写setup,更改可选的exe,读入预设
  */
  setup(){
    super.setup()
    this._myGui.opt("+AlwaysOnTop")
    this._myGui.show("x1100 y330")
    this.readPresetMenu("兰斯洛特")
  }

}

/* 运行当前脚本
*/
if (A_ScriptName == "圆桌骑士.2.ahk"){
  ;方向检测对象
  face := FaceDetector() 
  ;宏对象,有界面时第二个参数可以随便写
  macro := KnightsMacro(face, []) 
  ;新建界面并设定
  ui := KnightsUI(macro)
  ui.setup()
  ui._exeComboBox.Choose(MyEmu.MAME)
  ui._gameKeyEdit.Text := "x,d,space"
  ;ui._hotkeyEdit.Text := "~a,~w,~e,~c"
  ui._hotkeyEdit.Text := "~s,~a,~e,~f"
  ui._fpsEdit.Text := 6
  ui._testEdit.Text := "200,60,400"
}